//
//  BSKAddButtonAnimation.m
//  TextNote
//
//  Created by rimi on 16/5/16.
//  Copyright © 2016年 BlueSky. All rights reserved.
//

/*
 *   点击新建按钮Push到新建页面以及从新建页面POP到主页面的转场动画
 */

#import "BSKAddButtonAnimation.h"
#import "BSKHomeViewController.h"
#import "math.h"

@implementation BSKAddButtonAnimation


- (NSTimeInterval)transitionDuration:(nullable id <UIViewControllerContextTransitioning>)transitionContext{
	return 0.4;
}
- (void)animateTransition:(id <UIViewControllerContextTransitioning>)transitionContext{
	
	if (_isPush) {
		
		BSKHomeViewController * Vfrom = [transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey];
		UIViewController * Vto = [transitionContext  viewControllerForKey:UITransitionContextToViewControllerKey];
		
		[[transitionContext containerView]addSubview:Vfrom.view];
		[[transitionContext containerView]addSubview:Vto.view];
		UIView * addButton = [[UIView alloc]initWithFrame:Vfrom.addNoteButton.frame];
		UIView * maskview = [[UIView alloc]initWithFrame:Vfrom.addNoteButton.frame];
		addButton.layer.backgroundColor = Vfrom.addNoteButton.layer.backgroundColor;
		maskview.layer.backgroundColor = Vfrom.addNoteButton.layer.backgroundColor;
		
		addButton.layer.cornerRadius = Vfrom.addNoteButton.layer.cornerRadius;
		maskview.layer.cornerRadius = Vfrom.addNoteButton.layer.cornerRadius;
		[[transitionContext containerView]addSubview:addButton];
		
		CGFloat scale = sqrt(pow([UIScreen mainScreen].bounds.size.height,2)+pow([UIScreen mainScreen].bounds.size.width, 2))/(addButton.frame.size.width/2);
		
		addButton.alpha = 1;
		Vto.view.layer.mask = maskview.layer;
		[UIView animateWithDuration:[self transitionDuration:transitionContext] animations:^{
			addButton.alpha = 0;
			addButton.transform = CGAffineTransformMakeScale(scale,scale);
			maskview.transform = CGAffineTransformMakeScale(scale,scale);
		} completion:^(BOOL finished) {
			[transitionContext completeTransition:YES];
			Vto.view.layer.mask = nil;
			[addButton removeFromSuperview];
		}];
	}else{
		
		UIViewController * Vfrom = [transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey];
		BSKHomeViewController *Vto =[transitionContext  viewControllerForKey:UITransitionContextToViewControllerKey];

		
		[[transitionContext containerView]addSubview:Vto.view];
		[[transitionContext containerView]addSubview:Vfrom.view];
		UIView * addButton = [[UIView alloc]initWithFrame:Vto.addNoteButton.frame];
		UIView * maskview = [[UIView alloc]initWithFrame:Vto.addNoteButton.frame];
		addButton.layer.backgroundColor = Vto.addNoteButton.layer.backgroundColor;
		maskview.layer.backgroundColor = Vto.addNoteButton.layer.backgroundColor;
		
		addButton.layer.cornerRadius = Vto.addNoteButton.layer.cornerRadius;
		maskview.layer.cornerRadius = Vto.addNoteButton.layer.cornerRadius;
		[[transitionContext containerView]addSubview:addButton];
		
		CGFloat scale = sqrt(pow([UIScreen mainScreen].bounds.size.height,2)+pow([UIScreen mainScreen].bounds.size.width, 2))/(addButton.frame.size.width/2);
		
		addButton.transform = CGAffineTransformMakeScale(scale,scale);
		maskview.transform = CGAffineTransformMakeScale(scale,scale);
		
		addButton.alpha = 0;
		Vfrom.view.layer.mask = maskview.layer;
		[UIView animateWithDuration:[self transitionDuration:transitionContext] animations:^{
			addButton.alpha = 1;
			addButton.transform = CGAffineTransformMakeScale(1,1);
			maskview.transform = CGAffineTransformMakeScale(1,1);
		} completion:^(BOOL finished) {
			[transitionContext completeTransition:YES];
			Vfrom.view.layer.mask = nil;
			[addButton removeFromSuperview];
		}];

	}
	
}

@end
